# Create your views here.
from django.template.loader import get_template
from django.template import Context, RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
from cis525_presentation_app.models import Student
from forms import StudentForm, RegisterForm

def helloWorld(request):
    return HttpResponse('Hello World')

def helloData(request, digit):
    return HttpResponse('Hello World ' + digit)

def classListRend(requst):
    first = 'Jason'
    last = 'Richmond'
    email = 'jason@richmond.net'
    t = get_template('classList.html')
    html = t.render(Context({'firstName': first, 'lastName': last, 'email': email}))
    return HttpResponse(html)

def classList(request):
    first = 'Jason'
    last = 'Richmond'
    email = 'jason@richmond.net'
    return render_to_response('classList.html', {'firstName': first, 'lastName': last, 'email': email})

def classListDb(request):
    return render_to_response('classlistdb.html', {'students': Student.objects.all()})

def addStudent(request):
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            return render_to_response('addstudent.html', {'form': form, 'data':cd}, context_instance=RequestContext(request))
    else:
        form = StudentForm()
    return render_to_response('addstudent.html', {'form': form}, context_instance=RequestContext(request))

def register(request):
    if request.method == 'POST':
        post = True
        form = RegisterForm(request.POST)
        if form.is_valid():
            #add data to database
            data = form.cleaned_data            
            student = Student(umid=data['umid'], firstName=data['firstName'], lastName=data['lastName'], projectTitle=data['title'], email=data['email'], phone=data['phone'])
            student.save()
    else:
        post = False    
        form = RegisterForm()
    return render_to_response('register.html', {'form':form, 'post':post}, context_instance=RequestContext(request))

def index(request):
    return HttpResponse('Home Page')
    